import urllib2
from BeautifulSoup import BeautifulSoup
import re


url = 'http://www.nuoqiu.com/column/%d.html'
page_url = 'http://www.nuoqiu.com/column/%d/0/%d.html'
MAXCAT = 15

def getStoryList(p,i):
    story = []
    filename = '%d.txt' % i
    f = file(filename,'a')
    soup = BeautifulSoup(p,fromEncoding="utf-8")
    links = soup('li')
    for link in links:
        print link.string
#    print p
#    for link in links:
#        story.append(str(link.string))
#        f.write(str(link.string)+'\n')
    f.close
    return story

def getPageNum(p):
    soup = BeautifulSoup(p,fromEncoding="utf-8")
    l = soup.find('div',{'class':'pageviewp'}).findAll('li',{'class':'info'})
    b = re.compile(r'^\d{1}/(\d+)')
    d = b.search(str(l[0].string)).groups()[0]
    return d
all_story={}
for i in range(1,MAXCAT):
    newUrl = url % i
    #print newUrl
    page = urllib2.urlopen(newUrl)
    pagenum = getPageNum(page)
    print pagenum
    for j in range(1,int(pagenum)):
        newPageUrl = page_url % (i,j)
        page = urllib2.urlopen(newPageUrl)
        story_list = getStoryList(page,i)
        #print story_list






